package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.PickUpCodeWriteOffReq;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.PickUpCodeWriteOffCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.RefundService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogPo;
import com.ymttest.database.sharding.db.sharding.model.PickInStorePo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 发货 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SqPickUpCodeWriteOff {

	private static PickUpCodeWriteOffReq pickUpCodeWriteOffReq;
	private static PickUpCodeWriteOffCall pickUpCodeWriteOffCall;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("自提订单核销");
	}

	@Before
	public void caseUp() {

		pickUpCodeWriteOffReq = new PickUpCodeWriteOffReq();
		pickUpCodeWriteOffCall = new PickUpCodeWriteOffCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(pickUpCodeWriteOffCall.getOpurl());
		
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verifyPickupStore(long orderId) {
		
		Logger.comment("验证pick_in_store表");
		
		orderShardingWapper.clearCache();
		
		PickInStorePo pickInStorePo = orderShardingWapper
				.selectPickInStorebyOrderId(orderId).get(0);

		Logger.verifyEquals((byte) 3, pickInStorePo.getPickupStatus(),
				"验证PickupStatus");
		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(pickInStorePo.getPickedUpTime()) < 2,
				"验证数据库PickedUpTime跟系统当前时间差为2分钟内");
		
		Logger.comment("验证order_status_log表");
		OrderStatusLogPo orderStatusLogPo = orderShardingWapper.selectOrderStatusLogByOrderId(orderId);
		
		Logger.verifyEquals(1002, orderStatusLogPo.getOrderStatus(),
				"验证OrderStatus");
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqPickUpCodeWriteOff_001() {
		Logger.start(false, "商家通知自提，自提订单核销");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, 4321L, null);
			TradingSyncService.getSyncResult(orderId);

			SqTradingCallService.pickupConfirmReceiving(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			String pickUpCode = orderShardingWapper
					.selectPickInStorebyOrderId(orderId).get(0).getPickupCode();

			pickUpCodeWriteOffReq.setCsId(4321L);
			pickUpCodeWriteOffReq.setGoodsOwnerId(autotest_goodsOwnerId);
			pickUpCodeWriteOffReq.setPickUpCode(pickUpCode);
			pickUpCodeWriteOffReq.setOrderId(orderId);
			
			pickUpCodeWriteOffCall.setData(pickUpCodeWriteOffReq);
			pickUpCodeWriteOffCall.callService();
			
			this.verifyPickupStore(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqPickUpCodeWriteOff_002() {
		Logger.start(false, "买家确认自提，自提订单核销");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 0);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, 4321L, null);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			String pickUpCode = orderShardingWapper
					.selectPickInStorebyOrderId(orderId).get(0).getPickupCode();

			pickUpCodeWriteOffReq.setCsId(4321L);
			pickUpCodeWriteOffReq.setGoodsOwnerId(autotest_goodsOwnerId);
			pickUpCodeWriteOffReq.setPickUpCode(pickUpCode);
			pickUpCodeWriteOffReq.setOrderId(orderId);
			
			pickUpCodeWriteOffCall.setData(pickUpCodeWriteOffReq);
			pickUpCodeWriteOffCall.callService();
			
			this.verifyPickupStore(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqPickUpCodeWriteOff_003() {
		Logger.start(false, "买家确认自提，订单未发货，自提订单核销失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			String pickUpCode = orderShardingWapper
					.selectPickInStorebyOrderId(orderId).get(0).getPickupCode();

			pickUpCodeWriteOffReq.setCsId(4321L);
			pickUpCodeWriteOffReq.setGoodsOwnerId(autotest_goodsOwnerId);
			pickUpCodeWriteOffReq.setPickUpCode(pickUpCode);
			pickUpCodeWriteOffReq.setOrderId(orderId);
			
			pickUpCodeWriteOffCall.setData(pickUpCodeWriteOffReq);
			pickUpCodeWriteOffCall.callService();
			
			Logger.verifyEquals("198", pickUpCodeWriteOffCall.getCode(),
					"验证Code");
			Logger.verifyEquals("订单未发货，核销失败",
					pickUpCodeWriteOffCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqPickUpCodeWriteOff_004() {
		Logger.start(false, "买家确认自提，自提订单多次核销，核销失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, 4321L, null);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			String pickUpCode = orderShardingWapper
					.selectPickInStorebyOrderId(orderId).get(0).getPickupCode();

			pickUpCodeWriteOffReq.setCsId(4321L);
			pickUpCodeWriteOffReq.setGoodsOwnerId(autotest_goodsOwnerId);
			pickUpCodeWriteOffReq.setPickUpCode(pickUpCode);
			pickUpCodeWriteOffReq.setOrderId(orderId);
			
			pickUpCodeWriteOffCall.setData(pickUpCodeWriteOffReq);
			pickUpCodeWriteOffCall.callService();
			
			pickUpCodeWriteOffCall.callService();
			
			Logger.verifyEquals("198", pickUpCodeWriteOffCall.getCode(),
					"验证Code");
			Logger.verifyEquals("自提订单已完成，请勿重复操作",
					pickUpCodeWriteOffCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqPickUpCodeWriteOff_005() {
		Logger.start(false, "买家确认自提，错误的自提码，核销失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, 4321L, null);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");

			pickUpCodeWriteOffReq.setCsId(4321L);
			pickUpCodeWriteOffReq.setGoodsOwnerId(autotest_goodsOwnerId);
			pickUpCodeWriteOffReq.setPickUpCode("dsgklsjdkls");
			pickUpCodeWriteOffReq.setOrderId(orderId);
			
			pickUpCodeWriteOffCall.setData(pickUpCodeWriteOffReq);
			pickUpCodeWriteOffCall.callService();
			
			Logger.verifyEquals("198", pickUpCodeWriteOffCall.getCode(),
					"验证Code");
			Logger.verifyEquals("自提码有误，请核对后再试",
					pickUpCodeWriteOffCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqPickUpCodeWriteOff_006() {
		Logger.start(false, "买家确认自提，正在退货退款，核销失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 0);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			RefundService.createPartProductRefund(orderId, placeOrderResp
					.getOrderList().get(0).getOrderDetailPoList().get(0),
					autotest_goodsOwnerId, userId);
			
			Logger.comment("前置数据准备结束");
			
			String pickUpCode = orderShardingWapper
					.selectPickInStorebyOrderId(orderId).get(0).getPickupCode();

			pickUpCodeWriteOffReq.setCsId(4321L);
			pickUpCodeWriteOffReq.setGoodsOwnerId(autotest_goodsOwnerId);
			pickUpCodeWriteOffReq.setPickUpCode(pickUpCode);
			pickUpCodeWriteOffReq.setOrderId(orderId);
			
			pickUpCodeWriteOffCall.setData(pickUpCodeWriteOffReq);
			pickUpCodeWriteOffCall.callService();
			
			Logger.verifyEquals("198", pickUpCodeWriteOffCall.getCode(),
					"验证Code");
			Logger.verifyEquals("有未完成的退款单，核销失败",
					pickUpCodeWriteOffCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqPickUpCodeWriteOff_007() {
		Logger.start(false, "商家自提订单，正在退货退款，核销失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 1);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			SqTradingCallService.delivery(autotest_goodsOwnerId, orderId, 4321L, null);
			TradingSyncService.getSyncResult(orderId);

			SqTradingCallService.pickupConfirmReceiving(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			RefundService.createPartProductRefund(orderId, placeOrderResp
					.getOrderList().get(0).getOrderDetailPoList().get(0),
					autotest_goodsOwnerId, userId);
			
			Logger.comment("前置数据准备结束");
			
			String pickUpCode = orderShardingWapper
					.selectPickInStorebyOrderId(orderId).get(0).getPickupCode();

			pickUpCodeWriteOffReq.setCsId(4321L);
			pickUpCodeWriteOffReq.setGoodsOwnerId(autotest_goodsOwnerId);
			pickUpCodeWriteOffReq.setPickUpCode(pickUpCode);
			pickUpCodeWriteOffReq.setOrderId(orderId);
			
			pickUpCodeWriteOffCall.setData(pickUpCodeWriteOffReq);
			pickUpCodeWriteOffCall.callService();
			
			Logger.verifyEquals("198", pickUpCodeWriteOffCall.getCode(),
					"验证Code");
			Logger.verifyEquals("有未完成的退款单，核销失败",
					pickUpCodeWriteOffCall.getMsg(), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SqPickUpCodeWriteOff_008() {
		Logger.start(false, "买家确认自提，退货退款成功，核销成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrderWithPickupType(productBean,
							autotest_goodsOwnerId, 0);

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);
			
			SqTradingCallService.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			Thread.sleep(3000);
			TradingSyncService.getSyncResult(orderId);
			
			Long paymentId = orderShardingWapper.selectOrderbyOrderId(orderId)
					.getPaymentId();
			
			RefundService.createPartProductRefundSuccess(orderId,
					placeOrderResp.getOrderList().get(0).getOrderDetailPoList()
							.get(0), autotest_goodsOwnerId, paymentId, userId);
			
			Logger.comment("前置数据准备结束");
			
			String pickUpCode = orderShardingWapper
					.selectPickInStorebyOrderId(orderId).get(0).getPickupCode();

			pickUpCodeWriteOffReq.setCsId(4321L);
			pickUpCodeWriteOffReq.setGoodsOwnerId(autotest_goodsOwnerId);
			pickUpCodeWriteOffReq.setPickUpCode(pickUpCode);
			pickUpCodeWriteOffReq.setOrderId(orderId);
			
			pickUpCodeWriteOffCall.setData(pickUpCodeWriteOffReq);
			pickUpCodeWriteOffCall.callService();
			
			this.verifyPickupStore(orderId);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
